import Mock from 'mockjs'

// 房源数据类型定义
export interface HouseInfo {
  id: number
  image: string
  hasVR: boolean
  isRecommended: boolean
  title: string
  rooms: string
  area: string
  direction: string
  floor: string
  price: string
  unit: string
}

// 房源列表响应类型
export interface HouseListResponse {
  code: number
  message: string
  data: {
    list: HouseInfo[]
    total: number
  }
}

// 基础房源信息
const baseHouseData = [
  {
    title: '碧桂园天越府南苑',
    image:
      'https://ts1.tc.mm.bing.net/th/id/R-C.f58293bbae26715956c48feceee5b9c8?rik=jqhhYin%2b9paQ2w&riu=http%3a%2f%2fimg95.699pic.com%2fphoto%2f50128%2f0316.jpg_wh860.jpg&ehk=jLCVk4LRWM4yW%2bw%2b1vAThUK35Um6Pc1VjbLWtEZA%2fHk%3d&risl=&pid=ImgRaw&r=0'
  },
  {
    title: '万科金域蓝湾',
    image:
      'https://ts1.tc.mm.bing.net/th/id/R-C.f58293bbae26715956c48feceee5b9c8?rik=jqhhYin%2b9paQ2w&riu=http%3a%2f%2fimg95.699pic.com%2fphoto%2f50128%2f0316.jpg_wh860.jpg&ehk=jLCVk4LRWM4yW%2bw%2b1vAThUK35Um6Pc1VjbLWtEZA%2fHk%3d&risl=&pid=ImgRaw&r=0'
  },
  {
    title: '恒大御景湾',
    image:
      'https://ts1.tc.mm.bing.net/th/id/R-C.f58293bbae26715956c48feceee5b9c8?rik=jqhhYin%2b9paQ2w&riu=http%3a%2f%2fimg95.699pic.com%2fphoto%2f50128%2f0316.jpg_wh860.jpg&ehk=jLCVk4LRWM4yW%2bw%2b1vAThUK35Um6Pc1VjbLWtEZA%2fHk%3d&risl=&pid=ImgRaw&r=0'
  },
  {
    title: '绿城桂花城',
    image:
      'https://ts1.tc.mm.bing.net/th/id/R-C.f58293bbae26715956c48feceee5b9c8?rik=jqhhYin%2b9paQ2w&riu=http%3a%2f%2fimg95.699pic.com%2fphoto%2f50128%2f0316.jpg_wh860.jpg&ehk=jLCVk4LRWM4yW%2bw%2b1vAThUK35Um6Pc1VjbLWtEZA%2fHk%3d&risl=&pid=ImgRaw&r=0'
  },
  {
    title: '保利香槟国际',
    image:
      'https://ts1.tc.mm.bing.net/th/id/R-C.f58293bbae26715956c48feceee5b9c8?rik=jqhhYin%2b9paQ2w&riu=http%3a%2f%2fimg95.699pic.com%2fphoto%2f50128%2f0316.jpg_wh860.jpg&ehk=jLCVk4LRWM4yW%2bw%2b1vAThUK35Um6Pc1VjbLWtEZA%2fHk%3d&risl=&pid=ImgRaw&r=0'
  }
]

// Mock 房源列表数据 - 修改为从URL参数获取数据
const houseList = Mock.mock(/\/api\/house\/list(\?.*)?$/, 'get', (options: any) => {
  // 解析URL参数
  const url = new URL(options.url, 'http://localhost')
  const projectId = url.searchParams.get('project_id')
  const buildingId = url.searchParams.get('building_id')
  const page = parseInt(url.searchParams.get('page') || '1')
  const pageSize = parseInt(url.searchParams.get('pageSize') || '20')

  console.log('Mock houseList called with params:', { projectId, buildingId, page, pageSize })

  // 根据参数生成不同的数据
  const seed = projectId ? projectId.charCodeAt(0) : 1
  const buildingSeed = buildingId ? parseInt(buildingId) : 1

  // 预定义的选项数组
  const roomOptions = ['1室1厅', '2室2厅', '3室2厅', '4室2厅', '5室3厅']
  const directionOptions = ['南北', '东西', '东南', '西南', '朝南']
  const floorOptions = [
    '1. 沿街采光视野开阔',
    '1. 房东花了二十几万装修',
    '1. 宽厅大飞机户型，采光好',
    '1. 南北通透，格局方正',
    '1. 精装修，拎包入住',
    '1. 楼层好，采光充足',
    '1. 户型方正，空间利用率高'
  ]

  // 根据参数确定总数和当前页数据
  const baseTotal = 50 + (seed % 100) + buildingSeed * 10 // 根据项目ID和楼栋ID确定总数
  const total = Math.min(baseTotal, 200) // 最多200条

  // 计算当前页的数据
  const startIndex = (page - 1) * pageSize
  const endIndex = Math.min(startIndex + pageSize, total)
  const currentPageSize = endIndex - startIndex

  // 生成当前页的房源数据
  const list: HouseInfo[] = []
  for (let i = 0; i < currentPageSize; i++) {
    const index = startIndex + i
    const itemSeed = seed + buildingSeed + index

    list.push({
      id: index + 1,
      image: baseHouseData[itemSeed % baseHouseData.length].image,
      hasVR: itemSeed % 3 === 0, // 每3个一个VR
      isRecommended: itemSeed % 5 === 0, // 每5个一个推荐
      title: baseHouseData[itemSeed % baseHouseData.length].title,
      rooms: roomOptions[itemSeed % roomOptions.length],
      area: `${(80 + (itemSeed % 120) + Math.random() * 10).toFixed(1)}平`,
      direction: directionOptions[itemSeed % directionOptions.length],
      floor: floorOptions[itemSeed % floorOptions.length],
      price: `${80 + (itemSeed % 220)}万`,
      unit: `${(0.8 + (itemSeed % 7) * 0.1).toFixed(1)}万/平`
    })
  }

  return {
    code: 200,
    message: 'success',
    data: {
      list,
      total
    }
  }
})

export default {
  houseList
}
